<?php

class Backoffice_BackofficeController extends Fw_Config 
{

	public function indexAction() 
    {
		$this->validateSession();
	}
	
	public function loginAction()
	{
		
		require_once APPLICATION_PATH . 'modules/fw-user/models/FwUser.php';
		$users = new Model_FwUser();
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('signin');
            if ($formData) {
				$username = $this->_request->getPost('username');
				$passwd   = md5($this->_request->getPost('passwd'));
				
				$auth = Zend_Auth::getInstance();
				$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(), 'fwuser');
				$authAdapter->setIdentityColumn('username')
							->setCredentialColumn('passwd');
				$authAdapter->setIdentity($username)
							->setCredential($passwd);
							
				$result = $auth->authenticate($authAdapter);
				if($result->isValid()) {
					$storage = new Zend_Auth_Storage_Session();
					$storage->write($authAdapter->getResultRowObject(array('id','username','name','email','user_type','user_status')));
					$this->_redirect('fw-user/fw-user/list');
				} else {
					$this->view->errorMessage = 'Invalid User/Pass';
				}
			}
		}
	}
	
	public function logoutAction()
	{
		$storage = new Zend_Auth_Storage_Session();
		$storage->clear();
		$this->_redirect('backoffice/backoffice/index');
	}
}
